package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RHaveResourcesBean;
import com.xoms.admin.controller.res.bean.RResourceBean;
import com.xoms.admin.controller.res.bean.RRoleBean;
import com.xoms.admin.controller.springmvc.req.AddRoleResourcesParam;
import com.xoms.admin.controller.springmvc.req.DelResourceParam;
import com.xoms.admin.controller.springmvc.req.DelRoleParam;
import com.xoms.admin.controller.springmvc.req.DelRoleResourcesParam;
import com.xoms.admin.controller.springmvc.req.DisableResourceParam;
import com.xoms.admin.controller.springmvc.req.DisableRoleParam;
import com.xoms.admin.controller.springmvc.req.EditResourceParam;
import com.xoms.admin.controller.springmvc.req.EditRoleParam;
import com.xoms.admin.controller.springmvc.req.EditSorts2ResourceParam;
import com.xoms.admin.controller.springmvc.req.EditSorts2RoleParam;
import com.xoms.admin.controller.springmvc.req.EnableResourceParam;
import com.xoms.admin.controller.springmvc.req.EnableRoleParam;
import com.xoms.admin.controller.springmvc.req.GetResourcesParam;
import com.xoms.admin.controller.springmvc.req.GetRoleResourcesParam;
import com.xoms.admin.controller.springmvc.req.GetRolesParam;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.ResourceService;
import com.xoms.admin.service.RoleService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

@Component
public class RoleController {
	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	RoleService roleServiceImpl;

	@Autowired
	ResourceService resourceServiceImpl;
	
	/**
	 * 编辑角色，新建、更新；
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RRoleBean> editRole(EditRoleParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RRoleBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			int roleId = NumUtil.toInt(param.getId(), -1);// 为-1则新建
			String name = param.getName();// 姓名
			int isEnable = NumUtil.toInt(param.getIsEnable(), 1);// 默认启用
			String desc = param.getDesc();// 描述
			int sort = NumUtil.toInt(param.getSort(), 1);//
			//成功后返回编辑后角色
			RRoleBean data = roleServiceImpl.edit(account, roleId, name, isEnable, desc, sort);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除角色
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delRole(DelRoleParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int roleId = NumUtil.toInt(param.getRoleId(), -1);// 要删除的角色id

			boolean flag = roleServiceImpl.del(account, roleId);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取角色列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RRoleBean> getRoles(GetRolesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RRoleBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int pagesize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取角色列表
			PageResult<RRoleBean> resData = roleServiceImpl.getRoles(account, key, page, pagesize);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 启用角色
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult enableRole(EnableRoleParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int roleId = NumUtil.toInt(param.getId(), -1);// 要启用角色id
			
			boolean flag = roleServiceImpl.enable(account, roleId);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;

	}
	
	/**
	 * 禁用角色
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult disableRole(DisableRoleParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int roleId = NumUtil.toInt(param.getId(), -1);// 要禁用角色id
			
			boolean flag = roleServiceImpl.disable(account, roleId);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑权限：新建\更新
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RResourceBean> editResource(EditResourceParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RResourceBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			int rid = NumUtil.toInt(param.getRid(), -1);// 为-1则新建
			String name = param.getName();// 姓名
			int parentId =  NumUtil.toInt(param.getPid(), 0);// 父节点
			String url = param.getUrl();
			int type = NumUtil.toInt(param.getType(), -1);// 权限类型：1菜单2按钮操作
			int level = NumUtil.toInt(param.getLevel(), -1);// 级别：1一级菜单2二级菜单3按钮
			int isEnable = NumUtil.toInt(param.getIsEnable(), -1);// 默认启用
			String resDesc = param.getResDesc();
			int sort = NumUtil.toInt(param.getSort(), 0);// 排序序号
			
			//成功后返回编辑后权限
			RResourceBean data = resourceServiceImpl.edit(account, rid, name, parentId, url, type, level, isEnable, resDesc, sort);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除资源
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delResource(DelResourceParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int rid = NumUtil.toInt(param.getRid(), -1);// 要删除的部门id

			boolean flag = resourceServiceImpl.del(account, rid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取权限列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RResourceBean> getResources(GetResourcesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RResourceBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int pagesize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取权限列表
			PageResult<RResourceBean> resData = resourceServiceImpl.getResources(account, key, page, pagesize);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 启用权限资源
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult enableResource(EnableResourceParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int rid = NumUtil.toInt(param.getId(), -1);// 要启用权限id

			boolean flag = resourceServiceImpl.enable(account, rid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 禁用权限资源
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult disableResource(DisableResourceParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int rid = NumUtil.toInt(param.getId(), -1);// 要禁用权限id

			boolean flag = resourceServiceImpl.disable(account, rid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 根据角色id获取角色下权限列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RHaveResourcesBean> getRoleResources(GetRoleResourcesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RHaveResourcesBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			int roleId = NumUtil.toInt(param.getRoleId(), -1);// 角色id
			
			// 获取权限列表
			PageResult<RHaveResourcesBean> resData = resourceServiceImpl.getRoleResources(account, roleId);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑角色权限
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult addRoleResources(AddRoleResourcesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int roleId = NumUtil.toInt(param.getRoleId(), -1);// 角色id
			String resourceIds = param.getRids();// 获取权限id数组

			boolean flag = roleServiceImpl.addRoleResources(account, roleId, resourceIds);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑角色排序
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult editSorts2Role(EditSorts2RoleParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String sorts = param.getSorts();
			int page = NumUtil.toInt(param.getPage(), 0);
			int pagesize = NumUtil.toInt(param.getPageSize(), 20);
			
			result.setFlag(roleServiceImpl.editSorts2Role(sorts, page, pagesize));
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑资源排序
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult editSorts2Resource(EditSorts2ResourceParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String sorts = param.getSorts();
			int page = NumUtil.toInt(param.getPage(), 0);
			int pagesize = NumUtil.toInt(param.getPageSize(), 20);
			
			result.setFlag(resourceServiceImpl.editSorts2Resource(sorts, page, pagesize));
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 删除角色权限
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delRoleResources(DelRoleResourcesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int roleId = NumUtil.toInt(param.getRoleId(), -1);// 角色id
			String resourceIds = param.getRids();// 获取权限id数组

			boolean flag = roleServiceImpl.delRoleResources(account, roleId, resourceIds);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
